package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.entity.Category;
import com.itheima.entity.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;



    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true) //将setmealCache名下所有的缓存数据清楚
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }


    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        //创建分页构造对象
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件 根据name进行模糊查询
        queryWrapper.like(name != null,Setmeal::getName,name);
        //添加排序条件 根据更新时间降序排列
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo,queryWrapper);

        //创建新的分页对象
        Page<SetmealDto> dtoPage = new Page<>();
        //进行对象拷贝
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        //获取基本分页查询的数据集合
        List<Setmeal> records = pageInfo.getRecords();
        ArrayList<SetmealDto> dtoArrayList = new ArrayList<>();
        //遍历菜品集合
        records.forEach(setmeal -> {
            //将Setmeal拷贝到SetmealDto
            SetmealDto dto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,dto);
            //查询所属分类
            Category c = categoryService.getById(setmeal.getCategoryId());
            dto.setCategoryName(c.getName());
            //放入集合
            dtoArrayList.add(dto);
        });
        //将dtoArrayList 添加到dtoPage中
        dtoPage.setRecords(dtoArrayList);
        //返回对象分页数据
        return R.success(dtoPage);
    }


    /**
     * 删除套餐
     * @param ids
     * @return
     * @RequestParam 声明不是创建集合对象  而是创建集合容器来接收数据
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true) //清楚setmealCache名下 所有的缓存数据
    public R<String> delete(@RequestParam List<Long> ids){

        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }

    /**
     * 修改套餐  数据回显
     *
     * @param
     * @return
     */
    @GetMapping("/{id}")
    @CacheEvict(value = "setmealCache", allEntries = true) //清除setmealCache名下  所有的缓存数据
    public R<SetmealDto> getDate(@PathVariable Long id){
        log.info("修改套餐数据:{}",id);
        SetmealDto date = setmealService.getDate(id);

        return R.success(date);
    }


    /**
     * 修改套餐
     * @param SetmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto SetmealDto){
        log.info("setmeal:{}",SetmealDto);
        setmealService.updateById(SetmealDto);
        return R.success("套餐信息修改成功");
    }



    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache" ,key = "#setmeal.categoryId+'_'+#setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 修改套餐售卖状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable Integer status, @RequestParam List<Long> ids){
        log.info("status:{}",status);
        log.info("ids:{}",ids);

        setmealService.updateBatchStatus(status,ids);

        return R.success("套餐状态修改成功");
    }

}
